<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author:  Zl
// +----------------------------------------------------------------------
namespace app\crm\model;

use app\admin\model\User;
use app\common\traits\ActionRecordTrait;
use think\facade\Db;
use app\admin\model\Common;
use think\response\Json;
use think\Validate;

class CustomerTypeModel extends Common
{
    use ActionRecordTrait;
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'customer';

    /**
     * 更改客户分类（A类，B类，C类，D类）模型
     * @param  array $request 请求参数
     * @return array
     */
    public function changeCustomerClassification($request)
    {
        $customer_id = $request['customer_id'];

        $customer = Db::name('customer')->where('id','=',$customer_id)->find();

        $vipInfo = Db::name('customer_vip_log')->where('customer_id','=',$customer_id)->find();

        if($vipInfo){

            $time = strtotime(date('Y-m-d',time()));

            if($vipInfo['over_time']<$time){
                // echo "降级";
                $res = $this->tierDown($customer,$vipInfo['id']);

                return $res;

            }
            
            if($customer['member_classification'] > $vipInfo['member_classification']){
                // echo "等级提升";

                $res = $this->upgrades($customer,$vipInfo['id']);

                return $res;
            }else{
                $res = $this->upgradesMoney($customer,$vipInfo['id']);
            }

        }else{

            $res = $this->addDate($customer);

            return $res;

        }

        // var_dump($res);
        
    }


    // 新增customer_vip_log数据
    public function addDate($customer)
    {
        echo "新增数据";
        $consumption = $customer['consumption'];
        $customer_id = $customer['id'];

        $rcp = $this->changeConsumption($consumption);

        $newData = [];

        $newData['customer_id'] = $customer_id;
        $newData['money'] = $consumption;
        $newData['member_classification'] = $rcp['vipCode'];
        $newData['begin_time'] = time();
        $newData['over_time'] = strtotime("+1 year");
        $newData['update_time'] = time();

        $id = Db::name('customer_vip_log')->insertGetId($newData);
        
        if($id){

            return true;

        }else{

            return false;

        }
    }

    // 调整数据-等级提升
    public function upgrades($customer,$id)
    {
        $newConsumption = $customer['consumption'];

        $rcp = $this->changeConsumption($newConsumption);

        $save = [];

        // $save['customer_id'] = $customer_id;
        $save['money'] = $newConsumption;
        $save['member_classification'] = $rcp['vipCode'];
        $save['begin_time'] = time();
        $save['over_time'] = strtotime("+1 year");
        $save['update_time'] = time();

        Db::name('customer_vip_log')->where('id',$id)->update($save);

        Db::name('customer')->where('id',$customer['id'])->update(['member_classification' => $rcp['vipCode']]);

        return true;

    }

    // 调整数据-等级提升
    public function upgradesMoney($customer,$id)
    {
        $newConsumption = $customer['consumption'];

        $save = [];

        $save['money'] = $newConsumption;

        Db::name('customer_vip_log')->where('id',$id)->update($save);

        return true;

    }

    // 调整数据-等级降低
    public function tierDown($customer,$id)
    {
        $member_classification = $customer['member_classification'] ?? 0;
        
        if($member_classification>0){

            switch ($member_classification) {
                case $member_classification='73':
                    $consumption='50000';
                    break;
                case $member_classification>='72':
                    $consumption = '15000';
                    break;
                case $member_classification>='71':
                    $consumption = '5000';
                    break;
                default:
                    $consumption = '0';
                    break;
            }

            $newConsumption = bcsub($customer['consumption'],$consumption,2);

        }else{

        }

        $rcp = $this->changeConsumption($newConsumption);

        $save = [];

        $save['money'] = $consumption;
        $save['member_classification'] = $rcp['vipCode'];
        $save['begin_time'] = time();
        $save['over_time'] = strtotime("+1 year");
        $save['update_time'] = time();

        Db::name('customer_vip_log')->where('id',$id)->update($save);

        Db::name('customer')->where('id',$customer['id'])->update(['member_classification' => $rcp['vipCode']]);

        return true;

    }

    public function changeConsumption($consumption)
    {
        switch ($consumption) {
            case $consumption>='50000':
                $vipCode = '73';
                $vip = 'A类';
                break;
            case $consumption>='15000':
                $vipCode = '72';
                $vip = 'B类';
                break;
            case $consumption>='5000':
                $vipCode = '71';
                $vip = 'C类';
                break;
            case $consumption>='500':
                $vipCode = '70';
                $vip = 'D类';
                break;
            default:
                $vipCode = '0';
                $vip = '';
                break;
        }

        return [
            'vipCode' => $vipCode,
            'vip' => $vip,
        ];
    }

}